#include "curve.h"
#include "../disp/surface.h"

namespace ShCo
{

class CurveImpl : public Impl<Curve>
{
public:
    void init();
    void exit();
    Vector<SsPoint*>::Type _pnts;
};

void CurveImpl::init()
{
}

void CurveImpl::exit()
{
    Vector<SsPoint*>::Type::iterator it = _pnts.begin();
    for (; it != _pnts.end(); ++it)
    {
        delete *it;
    }
    _pnts.clear();
}

Curve::Curve()
{
    SH_I(Curve);
    d->init();
}

Curve::~Curve()
{
    SH_E(Curve);
    d->exit();
}

void Curve::append(SsPoint* pnt)
{
    SH_D(Curve);
    d->_pnts.push_back(pnt);
}

bool Curve::draw(Surface* surface)
{
    SH_D(Curve);
    if (d->_pnts.empty())
        return true;
    SsCurve curve;
    curve.n = d->_pnts.size();
    curve.pnts = new SsPoint[curve.n];
    for (int i = 0; i < curve.n; ++i)
    {
        curve.pnts[i].x = d->_pnts[i]->x;
        curve.pnts[i].y = d->_pnts[i]->y;
    }
    surface->drawCurve(&curve);
    delete[] curve.pnts;
    return true;
}

} // namespace ShCo
